package com.example.chcrm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.chcrm.entity.common.ResponseMsg;
import com.example.chcrm.entity.common.ServletUtils;
import com.example.chcrm.entity.common.TArea;
import com.example.chcrm.entity.employee.EmployeeVo;
import com.example.chcrm.entity.vehicle.VehicleDeployFleetCv;
import com.example.chcrm.service.VehicleDeployFleetCvService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author 邹
 * @Date 2025/4/26 15:34
 * @Version 1.0
 */
@RestController
@RequestMapping("/deployFleet")
@CrossOrigin // 允许跨域访问
public class VehicleDeployFleetCvController {

    @Autowired
    private VehicleDeployFleetCvService fleetCvService;

    @PostMapping("/pageList")
    public ResponseMsg<IPage<VehicleDeployFleetCv>> pageList(@RequestBody VehicleDeployFleetCv fleet) {
//        ResponseMsg<Object> msg = new ResponseMsg<>();
//        Page<TArea> result = tAreaService.pageList(tArea);
//        msg.setData(result);
//        msg.success("查询成功！");
        //获取登录用户信息
        HttpServletRequest request = ServletUtils.getRequest();
        EmployeeVo empVo = request != null ? (EmployeeVo) request.getAttribute("empVo") : null;
        return fleetCvService.pageList(fleet);
    }

    @GetMapping("/detail/{id}")
    public ResponseMsg<Object> detail(@PathVariable String id) {
        HttpServletRequest request = ServletUtils.getRequest();
        EmployeeVo empVo = request != null ? (EmployeeVo) request.getAttribute("empVo") : null;
        return fleetCvService.detail(id);
    }

    @PostMapping("/add")
    public ResponseMsg<Object> add(@RequestBody VehicleDeployFleetCv vehicleDeploy) {
        ResponseMsg<Object> msg = new ResponseMsg<>();
        if (vehicleDeploy.getStartType() == 1) {
            if (!StringUtils.isNotBlank(vehicleDeploy.getStartPortName())) {
                msg.fail("操作失败，起运港不能为空");
                return msg;
            }
        } else {
            if (!StringUtils.isNotBlank(vehicleDeploy.getStartName())) {
                msg.fail("操作失败，起运地不能为空");
                return msg;
            }
        }
        if (! StringUtils.isNotBlank (vehicleDeploy.getFleetName())) {
            msg.fail("操作失败，请至少选择一个供应商");
            return msg;
        }
        HttpServletRequest request = ServletUtils.getRequest();
        EmployeeVo empVo = request != null ? (EmployeeVo) request.getAttribute("empVo") : null;
        return fleetCvService.add(vehicleDeploy, empVo);
    }

    @PostMapping("/loadCars")
    public ResponseMsg<Object> startLoading(@RequestBody VehicleDeployFleetCv deployDriver){
        ResponseMsg<Object> msg = new ResponseMsg<>();
        if(deployDriver == null){
            msg.fail("操作失败，数据不完整");
            return msg;
        }
        if(deployDriver.getId() == null){
            msg.fail("操作失败，公路调配id不能为空");
            return msg;
        }
        if(deployDriver.getStartTime() == null){
            msg.fail("操作失败，装车时间不能为空");
            return msg;
        }
        if(deployDriver.getCarList() == null || deployDriver.getCarList().size() <= 0){
            msg.fail("操作失败，未有委托车装车");
            return msg;
        }
        HttpServletRequest request = ServletUtils.getRequest();
        EmployeeVo empVo = request != null ? (EmployeeVo) request.getAttribute("empVo") : null;

        return fleetCvService.startLoading(deployDriver,empVo);
    }

    @PostMapping("/unloadCars")
    public ResponseMsg<Object> fulfilTransport(@RequestBody VehicleDeployFleetCv deployDriver){
        ResponseMsg<Object> msg = new ResponseMsg<>();
        if(deployDriver == null){
            msg.fail("操作失败，数据异常");
            return msg;
        }
        if(!StringUtils.isNotBlank(deployDriver.getId())){
            msg.fail("操作失败，操作id不能为空");
            return msg;
        }
        if(deployDriver.getEndTime() == null){
            msg.fail("操作失败，完成时间不能为空");
            return msg;
        }
        HttpServletRequest request = ServletUtils.getRequest();
        EmployeeVo empVo = request != null ? (EmployeeVo) request.getAttribute("empVo") : null;
        return fleetCvService.fulfilTransport(deployDriver,empVo);
    }
}
